package com.clinic.web.servlets;

import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.clinic.web.util.Util;
import com.fasterxml.jackson.databind.ObjectMapper;

@WebServlet("/SignIn")
public class SignIn extends HttpServlet {


	private static final long serialVersionUID = 1L;
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		String email_address = request.getParameter("email_address");
		String passwrd = request.getParameter("passwrd");
		Map<String, String> map = new HashMap<String, String>();
		map.put("email_address", email_address);
		map.put("passwrd", passwrd);
		String jsonLoginInfo = mapper.writeValueAsString(map);
		ResourceBundle bundle = ResourceBundle.getBundle("com/clinic/web/prop/clinicServices", Locale.getDefault());
		String result = Util.POST_Method(bundle.getString("serverURL")+"patientservices/signin", jsonLoginInfo);
		
		if(result.equals("Valid")){
			response.addCookie(new Cookie("email", email_address));
			response.sendRedirect("index.jsp");
		} else {			
			request.setAttribute("msg","Invalid Username or Password" );
			RequestDispatcher dispatcher = request.getServletContext().getRequestDispatcher("/signin.jsp");
			dispatcher.forward(request, response);
		}
	}
}